import 'package:animal/utils/screen_util.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'image_error.dart';
import 'image_loading.dart';

//通用的多图显示布局
class ImagesWidget extends StatelessWidget {
  List<String>? images;
  List<String>? types;
  double? width;
  double? height;
  String? tag;

  ImagesWidget({Key? key, this.images, this.width, this.types, this.tag}) : super(key: key) {
    types ??= images!.map((e) => "image").toList();
    //动态计算宽高 ScreenUtil().L(10) 为间距
    if (images!.length >= 3) width = (width! - 2 * ScreenUtil().L(7)) / 3;
    if (images!.length == 2) width = (width! - ScreenUtil().L(7)) / 2;
    if (images!.length == 1) {
      height = (width! * 0.6);
    } else {
      height = width;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Wrap(
      alignment: WrapAlignment.start,
      children: _images(),
      spacing: ScreenUtil().L(7),
      runSpacing: ScreenUtil().L(7),
    );
  }

  List<Widget> _images() {
    List<Widget> widgets = [];
    for (int i = 0; i < images!.length; i++) {
      widgets.add(ClipRRect(
        borderRadius: BorderRadius.circular(ScreenUtil().L(20)),
        child: Container(
          width: width,
          height: height,
          child: _imageWidget(images![i]),
        ),
      ));
    }
    return widgets;
  }

  _imageWidget(String url) {
    return InkWell(
      onTap: () {
        // Get.to(
        //   PhotoViewSimpleScreen(imageProvider: NetworkImage("$url"), heroTag: 'simple'),
        //   fullscreenDialog: true,
        //   opaque: false,
        // );
      },
      child: CachedNetworkImage(
        imageUrl: url,
        placeholder: (context, url) => const ImageLoadingWidget(),
        errorWidget: (controller, url, err) => ImageErrorWidget(),
        fit: images!.length == 1 ? BoxFit.fitHeight : BoxFit.cover,
        width: width,
        height: height,
        memCacheWidth: 500,
      ),
    );
  }

  _videoWidget(String url) {
    // DynaimcListController controller = Get.find(tag: tag);
    return InkWell(
      onTap: () {
        // Get.to(
        //   VideoPopWidget(title: "视频播放", child: VideoPlayUrlPage(url)),
        //   opaque: false,
        //   fullscreenDialog: true,
        //   preventDuplicates: false, //是否禁止打开同一界面
        // );
      },
      child: Container(
          color: Colors.blueGrey,
          child: Stack(
            children: [
              // VideoPlayer(controller.getPlayControllers(url)),
              Center(
                child: Container(
                  height: ScreenUtil().L(80),
                  width: ScreenUtil().L(80),
                  child: Image.asset("icon/play.png"),
                ),
              )
            ],
          )),
    );
  }
}
